package com.dididoctor.doctor.Activity.Order.DiagnosisSuggest;

import android.content.Context;

import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.Bean.Token;
import com.dididoctor.doctor.Http.MyHttpResponseHandler;
import com.dididoctor.doctor.MV.BasePresenter;
import com.dididoctor.doctor.MV.BusinessClient;
import com.dididoctor.doctor.MV.FastHttpResponseHandler;
import com.dididoctor.doctor.MV.Response;
import com.dididoctor.doctor.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称：
 * @包名称：
 * @创建人：李营
 * @创建时间：2016/4/25 16:40
 * @修改人：
 * @修改时间：2016/4/25 16:40
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class DiagnosisSuggestPresenter extends BasePresenter {
    private DiagnosisSuggestView view;

    public DiagnosisSuggestPresenter(Context context, DiagnosisSuggestView view) {
        super(context, view);
        this.view = view;
    }

    public void updateSuggest(String recId, String content, List<DiagnosisSuggestBean> beans) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getUserId());
        params.put("token", Token.getToken());
        params.put("recId", recId);
        params.put("result", content);
        JSONArray arr = new JSONArray();
        for (int i = 0; i < beans.size(); i++) {
            JSONObject obj = new JSONObject();
            try {
                obj.put("type", beans.get(i).getType());
                obj.put("content", beans.get(i).getContent());
                arr.put(obj);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        params.put("advices", arr);

        BusinessClient.post(ConstantValue.sentmdcresult, params, new MyHttpResponseHandler() {

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.getDataSucced();
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getDataFail();
            }
        });
    }

    public void getadviceper(String diseId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getUserId());
        params.put("token", Token.getToken());
        params.put("diseId", diseId);
        BusinessClient.post(ConstantValue.adviceper, params, new FastHttpResponseHandler() {

            private List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
            private List<DiagnosisSuggestBean> beans = new ArrayList<DiagnosisSuggestBean>();
            private String show = "";

            @Override
            public void onResponse(Response response) {
                if (response.isSucceed()) {
                    maps = response.getListData("advices");
                    show = Util.toString(response.getString("show"));
                    if (!"0".equals(show)) {
                        for (Map<String, String> map : maps) {
                            DiagnosisSuggestBean bean = new DiagnosisSuggestBean();
                            bean.setType(Util.toString(map.get("type")));
                            bean.setContent(Util.toString(map.get("cnt")));
                            beans.add(bean);
                        }
                    }
                    view.getAdviceper(beans, show);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
            }
        });
    }
}
